package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.common.utils.MybatisBatchUtils;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandCarCrossCity;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandCarTimes;
import com.corpgovernment.travelstandard.mapper.MbTravelStandCarCrossCityMapper;
import com.corpgovernment.travelstandard.mapper.MbTravelStandCarTimesMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class MbTravelStandCarTimesService {

    @Autowired
    private MbTravelStandCarTimesMapper mbTravelStandCarTimesMapper;

    @Autowired
    private MybatisBatchUtils mybatisBatchUtils;

    public MbTravelStandCarTimes selectByTsid(Long tsId) {
        Example example = new Example(MbTravelStandCarTimes.class);
        example.createCriteria().andEqualTo("tsid", tsId);
        return mbTravelStandCarTimesMapper.selectOneByExample(example);
    }

    public List<MbTravelStandCarTimes> selectByTsids(List<Long> tsIds) {
        Example example = new Example(MbTravelStandCarTimes.class);
        example.createCriteria().andIn("tsid", tsIds);
        return mbTravelStandCarTimesMapper.selectByExample(example);
    }

    public boolean saveBatch(List<MbTravelStandCarTimes> carTimesList) {
        if (CollectionUtils.isEmpty(carTimesList)) {
            return false;
        }
        long result = mybatisBatchUtils.batchUpdateOrInsert(carTimesList, MbTravelStandCarTimesMapper.class, (item, mbTravelStandCarTimesMapper) -> mbTravelStandCarTimesMapper.insertSelective(item));
        return result > 0;
    }

    public boolean save(MbTravelStandCarTimes mbTravelStandCarTimes) {
        long result = mbTravelStandCarTimesMapper.insertSelective(mbTravelStandCarTimes);
        return result > 0;
    }

    public boolean delByTsid(Long tsId) {
        Example example = new Example(MbTravelStandCarTimes.class);
        example.createCriteria().andEqualTo("tsid", tsId);
        int result = mbTravelStandCarTimesMapper.deleteByExample(example);
        return result > 0;
    }
}
